package com.shiliuzi.www.pojo.bo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfoBo {
    private Integer id;
    private Integer grade;
    private Integer upId;
    private String group;
    private String identity;
    private String role;
    private String userName;
    private String number;
    private static final ThreadLocal<UserInfoBo> threadLocal = new ThreadLocal<>();

    public UserInfoBo(Integer id) {
        this.id = id;
    }

    /**
     * 重写转换器
     * @param group
     */
    public void setGroup(Integer group) {
        if (group.equals(1)) this.group = "后台组";
        else if (group.equals(2)) this.group = "前端组";
        else if (group.equals(3)) this.group = "管理组";
    }

    public void setIdentity(Integer identity) {
        if (identity.equals(1)) this.identity = "队长";
        else if (identity.equals(2)) this.identity = "组长";
        else if (identity.equals(3)) this.identity = "队员";
    }

    /**
     * 设置字段值
     * @param group
     */
    public void setGroup(String group) {
        this.group = group;
    }
    public void setIdentity(String identity) {
        this.identity = identity;
    }

    /**
     * 保证线程安全
     * @param userInfoBo
     */
    public static void saveUser(UserInfoBo userInfoBo) {
        if (threadLocal.get() == null) {
            threadLocal.set(userInfoBo);
        }
    }

    /**
     * 获取用户进行操作
     * @return
     */
    public static UserInfoBo obtainUser() {
        return threadLocal.get();
    }

    /**
     * 每次执行业务之后，不再维护该对象
     */
    public static void removeUser() {
        if (threadLocal.get() != null) {
            threadLocal.set(null);
        }
    }
}
